<?php

class cntrlhome implements viewController {
	private $area;
	private $side;
	private $dependencies = array("JavaScript" => array("facebox.js"), "CSS" => array("facebox.css") );
	private $pageName = "Welcome to PicBoard";
	private $userObj;
	private $subscribedGroups;
	private $tools = "";
	
	public function __construct(user &$userObj){
		$this->userObj = $userObj;
		if($this->userObj->getAccessLevel() == -1 || $this->userObj->getAccessLevel() == ""){
			// User is not logged in
			
			$this->dependencies['JavaScript'][] = "login.js";
			
			$this->displayWelcomePage();
		} else {
			
			// Inject the JS for logged in home.
			$this->dependencies['JavaScript'][] = "home.js";
		
			// Display page for user displaying status
			$this->area = new template("area.userhome");
			
			$this->side = new template("control.homeSidebar");
			
			$shares = $this->userObj->getShares();
			
			$this->side->replaceAll(array(
				"firstName" => $this->userObj->getFirstName(),
				"lastName" => $this->userObj->getLastName(),
				"inbox-count" => $shares['inbox'],
				"outbox-count" => $shares['outbox']
			));
			
			if($this->userObj->getAccessLevel() == 1){
				$this->displayAdminHomepage();
			} else {
				$this->displayUserHomepage();
			}
		}
	}
	
	private function displayWelcomePage(){
		$this->area = new template("area.welcome");
		$this->side = new template("control.login");
	}
	
	private function displayUserHomepage(){
		$itemTemplate = new template("fragment.listDisplayItem");
		
		$groupOutput = $this->formatGroupsAndReturn( $itemTemplate );
		// Put the data into the template.
		$this->area->replace('groupList', $groupOutput);
		
		$bookOutput = $this->formatBooksAndReturn( $itemTemplate );
		$this->area->replace('bookList', $bookOutput);
		
		$chartOutput = $this->formatChartsAndReturn( $itemTemplate );
		// Put the data into the template.
		$this->area->replace('chartList', $chartOutput);

		$imageOutput = $this->formatImagesAndReturn($itemTemplate);
		$this->area->replace('imageSetList', $imageOutput);
	
		// Generate the sidebar.
		$end = "<br />" . $this->generateInstitutionList();
		
		$this->tools = new template("toolset.userHome");
		
		$this->side->replace('sideContent', $end);
	}
	
	private function displayAdminHomepage(){
		// Generate the main part of the page.
		
		$itemTemplate = new template("fragment.listDisplayItem");
		
		$groupOutput = $this->formatGroupsAndReturn( $itemTemplate );
		
		// Put the data into the template.
		$this->area->replace('groupList', $groupOutput);

		$bookOutput = $this->formatBooksAndReturn( $itemTemplate );
		$this->area->replace('bookList', $bookOutput);

		$chartOutput = $this->formatChartsAndReturn( $itemTemplate );
		// Put the data into the template.
		$this->area->replace('chartList', $chartOutput);
	
		$imageOutput = $this->formatImagesAndReturn($itemTemplate);
		$this->area->replace('imageSetList', $imageOutput);
	
		// Generate the sidebar.
		$end = "Logged in as a SuperUser<br /><br />";
		
		$end .= $this->generateInstitutionList();
		
		$this->tools = new template("toolset.adminHome");
		
		$this->side->replace('sideContent', $end);
	}
	
	
	
	
	private function formatChartsAndReturn(template &$itemTemplate){
		$chartOutput = "";
		
		$images = chart::getUserChartThumbs( $this->userObj->getuID() );
				
		foreach($this->userObj->getCharts() as $chart){
			
			$out = "<div class='shareLinkSide'><button class='share_link'><var id='lang_144_share'></var></button></div>";
			
			$out .= "<a href='chart?id=" . $chart['chID'] . "'>" . $chart['name'] . "</a>";
			
			if($chart['private'] == 1) $out .= "<img src='" . SYS_BASEURL ."/images/eye--exclamation.png' class='iconHelper' align='right' title='<var id='lang_189_privateChart'></var>' />";
			
			$chart['description'] = ($chart['description'] != "") ? "<p>" . $chart['description'] . "</p>" : "<p class='smgreyText'><var id='lang_201_noDesc'></var></p>"; 
			
			$out .= $chart['description'];

			$imageURL = (array_key_exists($chart['chID'], $images)) ? $images[$chart['chID']] : SYS_BASEURL . "/images/document.png";
			
			// Is this IE & SVG?
			$browser = util::getBrowser();
			if(array_key_exists("msie", $browser) && strpos($imageURL, '.svg') !== FALSE ){
				$imageURL = preg_replace("/svg/i", 'wmf', $imageURL);
				
				$imageTemplate = sprintf("<img src='%s' height='70' width='70' />", $imageURL);
			} else {
			
				// If this is a SVG we need to change the template.
				if(strpos($imageURL, '.svg') !== FALSE){
					// We have a SVG
					
					// We need to strip off the http:// of the URL.
					$imageURL = preg_replace("/(http:\/\/)/i", "", $imageURL);
					$format = '<embed src="http://access.ecs.soton.ac.uk/seb/PicBoard/SVGProxy.php?r=%s" type="image/svg+xml" height="70" width="70">';
			
				} else {
					$format = "<img src='%s' height='70' width='70' />";
				}
				$imageTemplate = sprintf($format, $imageURL);
			}
			
			
			$itemTemplate->replace("desc", $out);
			$itemTemplate->replace("image", $imageTemplate);
			$chartOutput .= $itemTemplate->get();
			$itemTemplate->reset();
		}	
		return $chartOutput;
		
	}
	
	private function formatBooksAndReturn(template &$itemTemplate){
		$bookOutput = "";
		
		$images = book::getUserBookThumbs( $this->userObj->getuID() );
		
		foreach($this->userObj->getBooks() as $book){
			
			$out = "<a href='book?id=" . $book['bID'] . "'>" . $book['name'] . "</a>";
			if($book['private'] == 1) $out .= "<img src='" . SYS_BASEURL ."/images/eye--exclamation.png' class='iconHelper' align='right' title='<var id='lang_279_bookPrivate'></var>' />";
			$out .= "<p>" . $book['description'] . "</p>";

			
			$imageURL = (array_key_exists($book['bID'], $images)) ? $images[$book['bID']] : SYS_BASEURL . "/images/document.png";

			// Is this IE & SVG?
			$browser = util::getBrowser();
			if(array_key_exists("msie", $browser) && strpos($imageURL, '.svg') !== FALSE ){
				$imageURL = preg_replace("/svg/i", 'wmf', $imageURL);
				
				$format = "<img src='%s' height='70' width='70' />";
				$imageTemplate = sprintf($format, $imageURL);		
			} else {
				// If this is a SVG we need to change the template.
				if(strpos($imageURL, '.svg') !== FALSE){
					// We have a SVG
					
					// We need to strip off the http:// of the URL.
					$imageURL = preg_replace("/(http:\/\/)/i", "", $imageURL);
					$format = '<embed src="http://access.ecs.soton.ac.uk/seb/PicBoard/SVGProxy.php?r=%s" type="image/svg+xml" height="70" width="70">';
			
				} else {
					$format = "<img src='%s' height='70' width='70' />";
				}
				$imageTemplate = sprintf($format, $imageURL);			
			}

			$itemTemplate->replace("desc", $out);
			$itemTemplate->replace("image", $imageTemplate);
			$bookOutput .= $itemTemplate->get();
			$itemTemplate->reset();
		}	
		return $bookOutput;		
	}
	
	private function formatGroupsAndReturn(template &$itemTemplate){
		$groupOutput = "";
		foreach($this->userObj->getGroups() as $group){
			if($group['active'] == 1){
				$out = "<a href='group?id=" . $group['group_gID'] . "'>" . $group['name'] . "</a>";
				$out .= "<p>" . $group['description'] . "</p>";

				$itemTemplate->replace("desc", $out);
				$itemTemplate->replace("image", "");
				$groupOutput .= $itemTemplate->get();
				$itemTemplate->reset();
			}
		}	
		return $groupOutput;
	}
	
	private function formatImagesAndReturn(template &$itemTemplate){
		$imagesOutput = "";
		
		foreach(images::getUserLists($this->userObj) as $imageset){
			$out = "<a href='imageset?id=" . $imageset['isID'] . "'>" . $imageset['name'] . "</a>";

			$itemTemplate->replace("desc", $out);
			$itemTemplate->replace("image", "<img src=\"{absolute-url}/images/document.png\" height=\"70\" width=\"70\">");
			$imagesOutput .= $itemTemplate->get();
			$itemTemplate->reset();
		}
		
		return $imagesOutput;
	}
	
	private function generateInstitutionList(){
		$end = "<div style=\"border:1px solid white; margin-bottom:5px;\" class=\"ui-corner-all\"><var id=\"lang_276_communities\"></var><ul id='networks'>";
		$i = 0;
		foreach($this->userObj->rtnInstitution() as $institution){
			if($institution['instID'] == 1) continue;
			$end .= "<li>" . $institution['name'];
			$end .= $institution['admin'] ? " (<var id='lang_159_admin'></var>) (<a id='inst" . $institution['instID'] . "' class='addUserToInst' href='#'><var id='lang_280_addUsers'></var></a>)" : "";
			$end .= $institution['active'] ? "" : " (<var id='lang_281_pending'></var>)";
			$end .= "</li>";
			$i++;
		}
		
		$end .= "</ul></div>";	
		return ($i > 0) ? $end : '';	
	}
	
	
	public function getSide(){
		return $this->side;
	}
	
	public function getArea(){
		return $this->area;
	}
	
	public function getDependencies(){
		return $this->dependencies;
	}

	public function getPageName(){
		return $this->pageName;
	}
	
	public function getTools(){
		return $this->tools;
	}
}

?>